package com.fortune.conf;

/**
 * @program: fortune
 * @description:
 * @author: wwb
 * @create: 2025-04-29 10:18
 */
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import static com.fortune.conf.GifCompressorFFmpeg.extremeCompress;
import static com.fortune.conf.PngCompressor.compressPng;
import static com.fortune.conf.Utils.getAllFiles;

public class ImageCompressor {

    /**
     * 压缩图片核心方法
     * @param inputPath   输入文件路径
     * @param outputPath  输出文件路径
     * @param quality     质量 (0-100)
     */
    public static void compressImage(String inputPath,
                                     String outputPath,
                                     int quality) throws Exception {

        List<String> command = new ArrayList<>();
        command.add("ffmpeg");

        // 关键修正参数
        command.add("-pattern_type");
        command.add("none");  // 禁用序列检测

        command.add("-i");
        command.add(inputPath);
        command.add("-q:v");
        command.add(String.valueOf(quality));
        command.add("-preset");
        command.add("slow");
        command.add("-y");
        command.add(outputPath);

        executeCommand(new ProcessBuilder(command));
    }

    // 执行命令并处理错误
    private static void executeCommand(ProcessBuilder processBuilder)
            throws IOException, InterruptedException {

        processBuilder.redirectErrorStream(true);
        Process process = processBuilder.start();

        StringBuilder output = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line).append("\n");
            }
        }

        int exitCode = process.waitFor();
        if (exitCode != 0) {
            throw new RuntimeException("压缩失败:\n" + output.toString());
        }
    }


    // 使用示例
    public static void main(String[] args) {
        //D:\wwb\1\fc_image\2025\4\28 133m

//        //获取日期 地址 2025/05/01/
//        String dateDir = Utils.getDateDir();
//        List<String> allFiles = getAllFiles(new File(Utils.getUncompressed() + dateDir));
//        allFiles.forEach(absolutePath -> {
//            try {
//                BufferedImage read = ImageIO.read(new File(absolutePath));
//                if(read != null){
//                    if(absolutePath.contains(".gif")){
//                        extremeCompress(absolutePath,absolutePath.replace("uncompressed","compressed"));
//                    }else if(absolutePath.contains(".png")){
//                        compressPng(absolutePath, absolutePath.replace("uncompressed","compressed"), 3, 0.8, false);
//                    }else {
//                        compressImage(absolutePath,absolutePath.replace("uncompressed","compressed"),80);
//                    }
//                }
//            } catch (Exception e) {
//                System.out.println("allFiles.forEach-->"+e.getMessage());
//            }
//        });


        try {
            // 压缩JPEG示例
            compressImage(
                    "D:\\wwb\\1\\fc_image\\uncompressed\\2025\\4\\29\\005166bbc74247559440ec7af4433d19.jpg",
                    "D:\\wwb\\1\\fc_image\\compressed\\2025\\4\\29\\0bc833ef66cc4c45a45442c5f8d32264.jpg",
                    85
            );

//            // 压缩PNG示例
//            compressImage(
//                    "input.png",
//                    "compressed.png",
//                    90,
//                    1.0,
//                    ImageFormat.PNG
//            );

            System.out.println("压缩完成！");
        } catch (Exception e) {
            System.err.println("错误: " + e.getMessage());
        }
    }
}